package com.yungee.controller;
import com.alibaba.fastjson.JSONObject;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yungee.flow.context.OrderContext;
import com.yungee.logic.TransferFileLogic;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.yungee.remote.FeignTestService;
import com.yungee.util.FileUtil;
import com.yungee.vo.resp.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@RestController
@RequestMapping(value = "/" ,produces={"application/json;charset=UTF-8"})
public class IndexController {

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private ApplicationContext context;

    @Autowired
    private TransferFileLogic transferFileLogic;

    @Autowired
    private FeignTestService feignTestService;

    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("monitor")
    public Map getMonitor(){
        Map<String,Object> poolInfo = new HashMap<>();
        ThreadPoolExecutor t = taskExecutor.getThreadPoolExecutor();
        poolInfo.put("thread.pool.core.size",t.getCorePoolSize());
        poolInfo.put("thread.pool.max.size",t.getMaximumPoolSize());
        poolInfo.put("thread.pool.thread.count",t.getPoolSize());
        poolInfo.put("thread.pool.active.count",t.getActiveCount());
        poolInfo.put("thread.pool.completed.taskCount",t.getCompletedTaskCount());
        poolInfo.put("thread.pool.task.count",t.getTaskCount());
        return poolInfo;
    }

    @PostMapping("sav")
    public String transferFile(Long cardId){
        String result =transferFileLogic.transferFile(cardId);
        return result;
    }

    @GetMapping("testFeign")
    public AjaxResult testFeign(String name,String age){
        AjaxResult ajaxResult = feignTestService.index(name, age);
        return ajaxResult;
    }
    @GetMapping("testFlow")
    public AjaxResult testFlow(String chainName){
        Map<String,Object> params = new HashMap<>();
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(chainName, params, OrderContext.class);
        OrderContext orderContext = liteflowResponse.getContextBean(OrderContext.class);
        log.info("执行结果：{}",liteflowResponse.isSuccess());
        log.info("上下文信息收集：{}", JSONObject.toJSON(orderContext));
        return AjaxResult.success(orderContext);
    }
    @GetMapping("changeExcutor")
    public String changeExcutor(){
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
        // 移除旧的Bean
        if(defaultListableBeanFactory.containsBeanDefinition("threadPoolTaskExecutor")) {
            defaultListableBeanFactory.removeBeanDefinition("threadPoolTaskExecutor");
        }

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ThreadPoolTaskExecutor.class);
        beanDefinitionBuilder.addPropertyValue("corePoolSize", 5);
        beanDefinitionBuilder.addPropertyValue("maxPoolSize", 10);
        beanDefinitionBuilder.addPropertyValue("queueCapacity", 20);
        beanDefinitionBuilder.addPropertyValue("rejectedExecutionHandler", new ThreadPoolExecutor.AbortPolicy());

        // 使用registerSingleton方法注册线程池到Spring容器中
//        defaultListableBeanFactory.registerBeanDefinition("threadPoolTaskExecutor", beanDefinitionBuilder.getBeanDefinition());
        return "ok";
    }
}
